//
//  RJDetailBox.m
//  AirQuality
//
//  Created by Po on 15/3/17.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJDetailBox.h"
@interface RJDetailBox () <UITableViewDataSource, UITableViewDelegate>
@property (retain, nonatomic) UITableView * tableView;         //表格
@property (retain, nonatomic) UIView *      backView;          //模糊背景

@property (retain, nonatomic) NSArray * detailArray;           //详情数组
@property (assign, nonatomic) CGRect trueFrame;                //实际Frame
@property (assign, nonatomic) RJDetailBoxDirection direction;  //动画方向

- (void)initInterfaceWithBoxFrame:(CGRect)rect;
@end

@implementation RJDetailBox

- (void)dealloc
{
    [_tableView release];
    [_backView release];
    [super dealloc];
}

- (instancetype)initWithBoxFrame:(CGRect)frame detail:(NSArray *)detailArray
{
    _trueFrame = frame;
    if ((_trueFrame.origin.x + _trueFrame.size.width) > ScreenWidth) {
        _trueFrame.origin.x = ScreenWidth - _trueFrame.size.width;
    }
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _detailArray = [[NSArray alloc] initWithArray:detailArray];
        _direction = DetailBoxDirectionDown;
        [self initInterfaceWithBoxFrame:frame];
    }
    return self;
}

+ (instancetype)detailBoxWithBoxFrame:(CGRect)frame detail:(NSArray *)detailArray
{
    return [[[RJDetailBox alloc] initWithBoxFrame:frame detail:detailArray] autorelease];;
}

#pragma mark - user-defined initialization function
- (void)initInterfaceWithBoxFrame:(CGRect)rect
{
    [self setHidden:YES];
    [self addSubview:[self getBackView]];
    [self addSubview:[self getTableView]];
    [_tableView setFrame:rect];
}

- (UITableView *)getTableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_tableView setScrollEnabled:NO];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
    }
    return _tableView;
}

- (UIView *)getBackView
{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [_backView setBackgroundColor:[UIColor blackColor]];
        [_backView setAlpha:0.5];
    }
    return _backView;
}
#pragma mark - function

- (void)show
{
    [self setHidden:NO];
    UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    if (_direction == DetailBoxDirectionTop) {
        _trueFrame.origin.y -= _trueFrame.size.height;
    }
    [self.tableView setFrame:_trueFrame];
}

- (void)showWithAnim
{
    [UIView animateWithDuration:0.3 animations:^{
        [self show];
    }];
}

- (void)setAnimDirection:(RJDetailBoxDirection)direction
{
    _direction = direction;
}

/**释放自身*/
- (void)freeSelf
{
    [self removeFromSuperview];
    [self.layer removeAllAnimations];
//    [self release];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_delegate) {
        [_delegate RJDetailBoxDelegate:self DidSelected:indexPath.row];
    }
    [self freeSelf];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _detailArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (CGRectGetHeight(_tableView.frame) / _detailArray.count);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdent = @"detailBoxCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
        cell.accessoryType = UITableViewCellAccessoryNone;
        [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
        [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
    }
    [cell.textLabel setText:_detailArray[indexPath.row]];
    return cell;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self freeSelf];
}
@end
